<?php
namespace Admin\Controller;
use Admin\Logic\Controller\MainController;
use Admin\Model\AdminUserModel;
use Admin\Model\RoleModel;

class AdminUserController extends MainController {
    protected $adminUserModel = null;
    public function _initialize(){
        $this->adminUserModel = new AdminUserModel();
        $this->roleModel = new RoleModel();
    }

    public function admin_index(){
        $this->assign ( 'list',  $this->adminUserModel->lists() );
        $this->display();
    }
    
    public function edit(){
        if(!IS_POST){
            $id = I('get.id',null,'int');
            $this->assign ( 'info',$this->adminUserModel->Find($id) );
            $this->assign ( 'rolefind',$this->adminUserModel->rolefind($id) );
            $this->assign ( 'roleinfo',$this->roleModel->lists() );
            $this->display();
        }else{
            $id = I('id');
            //var_dump($id);exit;
            if(empty($id)){
                $data['username'] = I("post.username");
                $data['password'] = md5(I("post.password"));
                $role_id=I("role_id");
                $data['remark'] = I("post.remark",'','string');
                $data['hid']=1;
                $res = $this->adminUserModel->userAdd($data);
                if($res){
                    $data_role['role_id']=$role_id;
                    $data_role['user_id']=$res;
                    $this->adminUserModel->userroleAdd($data_role);
                    $this->success('添加成功',U("AdminUser/admin_index"));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $data['username'] = I("post.username");
                $data['password'] = md5(I("post.password"));
                $role_id=I("role_id");
                $data['remark'] = I("post.remark",'','string');
                $data['hid']=1;                
                M("admin_user")->where("id='$id'")->save($data);
                $data_role['role_id']=$role_id;
                $data_role['user_id']=$id;
                M("user_role")->where("user_id='$id'")->save($data_role);              
                $this->success("保存成功",U("AdminUser/admin_index"));
                }              
            
        }        
    }
    
    public function delete(){
        $id = I('get.id',null,'int');
        if($id){
            $res = $this->adminUserModel->deleteList($id);
        }
        if($res){
            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }     
}